import javafx.stage.FileChooser;
import org.gdal.gdal.*;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.ogr;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import static org.gdal.gdalconst.gdalconstConstants.GA_ReadOnly;

public class Main {

    private File file;

    public static void main(String[] args) {
        long millis = System.currentTimeMillis();
        gdal.AllRegister();
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

        String fileName_tif = "E:\\TestInput\\TestInput2\\tucenghoudu.tif";
        Dataset hDataset = gdal.Open(fileName_tif, gdalconstConstants.GA_ReadOnly);
        if (hDataset == null) {
            System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());
            System.exit(1);
        }

        Driver hDriver = hDataset.GetDriver();
        System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
        System.out.println("Size is " + hDataset.getRasterXSize() + ", " + hDataset.getRasterYSize());
        System.out.println("Band is " + hDataset.getRasterCount());

        Band band = hDataset.GetRasterBand(1);

        // 获取波段的无数据值
        int rasterXSize = hDataset.getRasterXSize();
        int rasterYSize = hDataset.getRasterYSize();
        int buf[] = new int[rasterXSize];
        for (int i = 0; i < rasterYSize; i++) {
            band.ReadRaster(0, i, rasterXSize, 1, buf);    //读取一行数据
            // 下面是输出高程值
            for (int j = 0; j < rasterXSize; j++) {
                float v = buf[j];
//                if (v == 255) {
//
//                } else {
//                   System.out.print(v+ ", ");
//                }
            }
        }

        hDataset.delete();
        // 可选
        gdal.GDALDestroyDriverManager();

        long millis2 = System.currentTimeMillis();
        System.out.println(millis2-millis);

//        String option = "invdist:power=2.0:smoothing=1.0 -txe 85000 89000 -tye 894000 890000 -outsize 400 400 -of GTiff -ot Float64 -l dem dem.vrt dem.tiff";
//        gdal.GridCreate(option, );
//        ByteBuffer
    }
//
//    private static void readTif(Driver oDriver) {
//        File file = new File("D:\\TestDta\\images\\02c_79_107_20130725_14_fus.tif");
//
//        Dataset ds = gdal.Open(file.getPath(), GA_ReadOnly); // 读取数据到gdal的dataset中
//
//        int XSize = ds.getRasterXSize();
//
//        int YSize = ds.getRasterYSize(); // 获取栅格数据的长和宽
//
//        int count = ds.getRasterCount(); // 获取栅格数据的点的数量
//
//        System.out.println("X = " + XSize);
//        System.out.println("Y = " + YSize);
//        System.out.println("Z = " + count);
//
////        opts = gdal.GridOptions(
////                algorithm="invdistnn:power=2.0:smothing=0.0:radius=1.0:max_points=12:min_points=0:nodata=-10",
//
//    }
//
//    private static void readShqpe(Driver oDriver) {
//        File file = new File("D:\\TestDta\\XZJX.shp");
//        System.out.println(file.getPath());
//
//        //打开文件
//        DataSource ds = oDriver.Open(file.getPath());
//        org.gdal.ogr.Layer oLayer = ds.GetLayerByIndex(0);
//
//        System.out.println("name = " + oLayer.GetFeatureCount());
//    }
//
//    private static Driver driverInit() {
//        // 注册所有的驱动
//        ogr.RegisterAll();
//        // 为了支持中文路径，请添加下面这句代码
//        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
//        // 为了使属性表字段支持中文，请添加下面这句
//        gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
//        String strDriverName = "ESRI Shapefile";
//        Driver oDriver = ogr.GetDriverByName(strDriverName);
//        return oDriver;
//    }
}
